<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <!-- @on-load="onLoad" -->

      <template slot="menuRight">
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permissionList.exportBtn"
          @click="handleExport"
          >导出
        </el-button>
      </template>
      <template slot-scope="{row}" slot="menu">
        <el-button
          icon="el-icon-view"
          size="small"
          type="text"
          v-if="permissionList.viewBtn"
          @click.stop="goTo(row)"
          >查看
        </el-button>
      </template>
    </avue-crud>
     <div class="curd-foter">
      <div class="redFontBox">交易总金额：<span class="redFont">{{exhibition(countData.totalTradAmount) || 0}}</span> 元</div>
      <div class="redFontBox">交易总笔数：<span class="redFont">{{exhibition(countData.tradeCount) || 0}}</span> 笔</div>
      <div class="redFontBox">交易手续费：<span class="redFont">{{exhibition(countData.totalRFee) || 0}}</span> 元</div>
      <div class="redFontBox">垫资手续费：<span class="redFont">{{exhibition(countData.totalRFeeD0) || 0}}</span> 元</div>
      <div class="redFontBox">交易分润：<span class="redFont">{{exhibition(countData.totalProfitAmount) || 0}}</span> 元</div>
      <div class="redFontBox">垫资分润：<span class="redFont">{{exhibition(countData.totalProfitAmountD0) || 0}}</span> 元</div>
      <div class="redFontBox">总分润：<span class="redFont">{{exhibition(countData.allProfitAmount) || 0}}</span> 元</div>
      <!-- <div class="redFontBox">下级总分润金额：<span class="redFont">{{exhibition(countData.totalChildProfitAmount) || 0}}</span> 元</div>
      <div class="redFontBox">下级总Do分润金额：<span class="redFont">{{exhibition(countData.totalChildProfitAmountD0) || 0}}</span> 元</div>
     -->
    </div>
  </basic-container>
</template>

<script>
import {
  getListGroup as getList,
  getListCount,
  getExportGroup
} from "@/api/rb-service/merchantprofit/service_profit_day";
import { mapGetters } from "vuex";
import { downloadFileExcel } from "@/util/util";

export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        selection: true,
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth: 160, //弹出表单的标题的统一宽度
        searchLabelWidth: 100, //搜索框标题宽度
        menuWidth: 100, //操作栏宽度
        emptyBtnText:'重置',
        column: [
          {
            label: "商户编号",
            prop: "rpiNo",
            type: "input",
            search: true,
            width: 140,
            searchOrder: 4
          },
          {
            label: "门头名称",
            prop: "rpiName",
            type: "input",
            search: true,
            width: 200,
            searchOrder: 4
          },
          {
            label: "统计日期",
            prop: "dayProfit",
            type: "input",
            format:'yyyy-MM-dd',
            valueFormat:'yyyy-MM-dd',
            width: 140
          },
          {
            label: "订单金额",
            prop: "tradeAmount",
            type: "input",
            overHidden:true,
            minWidth:80,
          },
          {
            label: "交易笔数",
            prop: "tradeCount",
            type: "input",
            minWidth:70,
            overHidden:true,
          },
          {
            label: "交易手续费",
            prop: "totalHandling",
            type: "input",
            overHidden:true,
            minWidth:92,
          },
          {
            label: "垫资手续费",
            prop: "totalHandlingD0",
            type: "input",
            minWidth:92,
            overHidden:true,
          },
          {
            label: "本级交易分润",
            prop: "handlingProfit",
            type: "input",
            minWidth:100,
            overHidden:true,
          },
          {
            label: "本级垫资分润",
            prop: "handlingProfitD0",
            type: "input",
            minWidth:100,
            overHidden:true,
          },
          {
            label: "服务商总分润",
            prop: "allProfitAmount",
            type: "input",
            minWidth:100,
          },
          // {
          //   label: "确认状态",
          //   prop: "isPaymentName",
          //   type: "input",
          // },
          {
            label: "交易日期",
            prop: "timeData",
            type: "daterange",
            searchRange: true,
            // defaultTime:['00:00:00', '23:59:59'],
            format:'yyyy-MM-dd',
            valueFormat:'yyyy-MM-dd',
            startPlaceholder: "交易日期开始时间",
            endPlaceholder: "交易日期结束时间",
            search: true,
            rules: [
              {
                required: true,
                message: "请选择交易日期",
                trigger: "blur",
              },
            ],
            hide: true,
            display: false,
            searchSpan: 12,
            overHidden:true,
          },
        ],
      },
      data: [],
      countData: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.new_service_profit_day_view,false),
        exportBtn: this.vaildData(this.permission.new_service_profit_day_export,false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    exhibition() {
      return (val) => {
        if (val == "" || val == "-1") {
          return (val = 0);
        } else {
          return val;
        }
      };
    },
  },
  watch: {
    "search.timeData"(val) {
      if (val) {
        this.search.beginTime = val[0];
        this.search.endTime = val[1];
      } else {
        this.search.beginTime = "";
        this.search.endTime = "";
      }
    },
  },
  methods: {
    // 页面跳转详情
    goTo(row){
      this.$router.push({
        path: '/oemprofit/profit_datail',
        query: { 
          dayProfit: row.dayProfit,
          rpiNo: row.rpiNo,
          sourceTenantId: row.sourceTenantId,
          name: row.rpiName+'-服务商日汇总',
          nameTitle: row.rpiName,
          type:'merDay'
        }
      })
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        //   getDetail(this.form.id).then(res => {
        //     this.form = res.data.data;
        //   });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.refreshChange();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.refreshChange();
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if(this.query.isSelectChildArr){
        this.query.isSelectChild = this.query.isSelectChildArr[0];
      }

      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });

      // 获取统计
      getListCount(Object.assign(params, this.query)).then((res) => {
        this.countData = res.data.data;
      });
    },
    /* 服务商 导出excel */
    handleExport() {
      if (this.search.timeData[0]) {
        this.search.beginTime = this.search.timeData[0];
      }
      if (this.search.timeData[1]) {
        this.search.endTime = this.search.timeData[1];
      }
      if(this.search.isSelectChildArr){
        this.search.isSelectChild = this.search.isSelectChildArr[0];
      }

      let params ={
        ...this.search,
        current:'-1',
        size:'-1'
      }

      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
       getExportGroup(params).then((res) => {
            downloadFileExcel(res);
          },
          (error) => {
            window.console.log(error);
          }
        );
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>


<style lang="scss" scoped>
.curd-foter {
  padding-top: 10px;
  display: flex;
  flex-wrap: wrap;
}

.redFontBox {
  margin-right: 40px;
  .redFont {
    color: red;
  }
}
</style>

